<?php
namespace Crm\Table;

use Zend\Db\Sql\Select;
use Zend\Db\Sql\Where;

class VoucherTable extends AbstractTable
{
    protected $table = 'hy_voucher';
    protected $id = 'id';
    
    public function getVoucherList($query = array())
    {
        $select = new Select(array('v' =>$this->getTable()));
        
//         $select->join(
//             array('org' => 'hy_organization'),
//             'vt.org_id = org.org_id',
//             array('org_name') ,
//             $select::JOIN_LEFT
//         );
        if(!empty($query['vt_id'])) {
            $select->where(array('v.vt_id' => $query['vt_id']));
        }
        
        $select->order('v.id');
                 
        return $this->fetchPaginate($select,$query);
    }
//     public function updateItem($id,$data){
//         if(empty($id)){
//             throw new \Exception("id field is invalid!");
//         }
//         $item = array(
//             'vt_name'=>$data['name'],
//             'vt_price'=>$data['price'],
//             'vt_remark'=>$data['remark'],
//             'vt_start_time'=>$data['start_time'],
//             'vt_end_time'=>$data['end_time'],
//             'org_id'=>$data['org_id'],
//         );
//         if(isset($data['user_password'])){
//             $item['user_password'] = $data['user_password'];
//         }
//         return $this->update($item,array('user_id'=>$id));
//     }
    public function getVoucherByCode($code)
    {
        if(empty($code)){
            throw new \Exception("code field is invalid!");
        }
        $sql='SELECT * FROM `hy_voucher` WHERE `code` = :code';
        $statement=$this->getAdapter()->query($sql);
        $results=$statement->execute(array(':code'=>$code));
        $row=$results->current();
        return $row;
    }
    
    public function addItem($data){
    
        $data = array(
            'vt_id'=>$data['vt_id'],
            'code'=>$data['code'],
        );
        $this->insert($data);
        $id = $this->lastInsertValue;
        return $id;
    }
    public function deleteByVtId($vt_id){
        if(empty($id)){
            throw new \Exception("id field is invalid!");
        }
        return $this->delete(array('vt_id'=>$vt_id));
    
    }
    public function getOutExcelList($query = array())
    {
        $select = new Select(array('v' =>$this->getTable()));
    
        $select->join(
            array('vt' => 'hy_voucher_type'),
            'vt.vt_id = v.vt_id',
            array('vt_name','vt_price','vt_num','vt_remark','vt_start_time','vt_end_time') ,
            $select::JOIN_LEFT
            );
        $select->join(
            array('u' => 'hy_user'),
            'u.user_id = v.user_id',
            array('user_name') ,
            $select::JOIN_LEFT
            );
        if(!empty($query['vt_id'])) {
            $select->where(array('v.vt_id' => $query['vt_id']));
        }
    
        $select->order('v.id');
         
        $rowset = $this->selectWith($select);
        $list = $rowset->toArray();
    
        return $list;
    }
}